<template>
  <div class="div_padding">
    <el-form
      :inline="true"
      :model="formInline"
      ref="formInline"
      class="demo-form-inline"
      size="small"
    >
      <el-form-item label="关键字：" prop="url">
        <el-input v-model="formInline.url" placeholder="关键字"></el-input>
      </el-form-item>
      <el-form-item label="请求方式：" prop="method">
        <el-select v-model="formInline.method" placeholder="请选择请求方式">
              <el-option
                v-for="item in methodOptions"
                :key="item.value"
                :label="item.name"
                :value="item.value">
              </el-option>
              </el-select>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" v-permission="'Search'" type="primary" @click="filterData"
          >查询</el-button
        >
        <el-button icon="el-icon-refresh" plain @click="resetForm('formInline')"
          >重置</el-button
        >
        <el-button icon="el-icon-plus" v-permission="'Create'" type="success" @click="onSubmit"
          >添加白名单</el-button
        >
        <Insert ref="insert" @reloadTable="reloadTable"></Insert>
        <Edit
          ref="edit"
          @reloadTable="reloadTable"
        ></Edit>
      </el-form-item>
      <el-button
        style="float: right"
        icon="el-icon-refresh-right"
        circle
        @click="reloadTable"
      ></el-button>
    </el-form>
    <el-table
      :data="tableData"
      size="small"
      height="72vh"
      v-loading="loading"
      empty-text="查询结果为空!"
      element-loading-text="数据加载中..."
      element-loading-spinner="el-icon-loading"
      style="width: 100%; overflow-x: hidden"
      row-key="id"
      border
    >
      <el-table-column
        prop="id"
        label="ID"
        v-if="showId"
        align="center"
        width="180"
      >
      </el-table-column>
      <el-table-column label="序号" type="index" align="center" width="50">
      </el-table-column>
      <el-table-column prop="url" label="接口地址" align="center" width="220">
      </el-table-column>
      <el-table-column prop="method" label="请求方式" align="center" width="180">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.method == 0">GET</el-tag>
          <el-tag type="info" v-if="scope.row.method == 1">POST</el-tag>
          <el-tag type="warning" v-if="scope.row.method == 2">PUT</el-tag>
          <el-tag type="danger" v-if="scope.row.method == 3">DELETE</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="enable_datasource" label="数据权限认证" align="center" width="180">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.enable_datasource == true">激活</el-tag>
          <el-tag type="danger" v-if="scope.row.enable_datasource == false">未激活</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="description"
        label="备注"
        align="center"
        width="230"
      >
      </el-table-column>
      <el-table-column
        prop="update_datetime"
        label="更新时间"
        align="center"
        width="180"
      >
      </el-table-column>
      <el-table-column
        prop="create_datetime"
        label="创建时间"
        sortable
        align="center"
        width="180"
      >
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="300"
        prop="parent"
        fixed="right"
      >
        <template slot-scope="scope">
          <div v-if="scope.row.parent == null">
            <el-button
              type="primary"
              v-permission="'Update'"
              size="mini"
              @click="onEdit(scope.$index, scope.row)"
              icon="el-icon-edit"
              plain
              >修改</el-button
            >
            <el-button
              type="danger"
              v-permission="'Delete'"
              size="mini"
              icon="el-icon-delete"
              plain
              @click="onDelete(scope.row)"
              >删除</el-button
            >
          </div>
        </template>
      </el-table-column>
    </el-table>
    <div class="block" style="margin: 5px">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        background
        :current-page="cur_page"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>
<script>
import { getWhiteListData,delWhiteListData } from "@/api/whiteList";
import { successMsg, errorMsg } from "@/utils/tools";
import Insert from "./insert";
import Edit from "./edit";
export default {
  name: "WhiteList",
  components: {
    Insert,
    Edit,
  },
  data() {
    return {
      showId: false,
      loading: true,
      tableData: [],
      total: 0,
      cur_page: 1,
      pageSize: 10,
      methodOptions:[{
          name:'GET',
          value:0
        },{
          name:'POST',
          value:1
        },{
          name:'PUT',
          value:2
        },{
          name:'DELETE',
          value:3
        }],
      formInline: {
        url: "",
        method: "",
      },
    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    fetchData() {
      getWhiteListData().then((res) => {
        this.tableData = res.data.data;
        this.total = res.data.total;
      });
      this.loading = false;
    },
    onSubmit() {
      this.$refs.insert.dialogFormVisible = true;
    },
    onEdit(index, row) {
      this.$refs.edit.dialogFormVisible = true;
      this.$refs.edit.formInline = Object.assign({}, row); //formInline，将row的数据复制到editForm对象中，实现的是浅拷贝
    },
    onDelete(row) {
      this.$confirm("警告，谨慎操作!是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delWhiteListData(row.id).then((res) => {
            if (res.code === 2000) {
              successMsg(res.msg);
              this.reloadTable();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    reloadTable() {
      this.loading = true;
      this.fetchData();
    },
    filterData() {
      let params = {};
      if (this.formInline.url) {
        params.url = this.formInline.url;
      }
      if (this.formInline.method) {
        params.method = this.formInline.method;
      }
      params.page=this.cur_page
      params.limit=this.pageSize
      this.loading = true; 
      getWhiteListData(params).then((res) => {
        this.tableData = res.data.data;
        this.loading = false;
      });
    },
      handleSizeChange(val) {
        this.pageSize=val
        this.loading=true
        this.filterData()
      },
      handleCurrentChange(val) {
        this.cur_page=val
        this.loading=true
        this.filterData()
      },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.reloadTable();
    },
  },
};
</script>
<style lang="scss" scoped>
.div_padding {
  padding: 10px 10px;
}
</style>
